//
//  MainViewController.m
//  花花时尚
//
//  Created by 霍文轩 on 15/8/27.
//  Copyright (c) 2015年 霍文轩. All rights reserved.
//

/*
 * 创建容器
 *
 */
#import "MainViewController.h"
#import "FashionViewController.h"
#import "LeftViewController.h"
#import "AppDelegate.h"
#import "YRSideViewController.h"

#import "Reachability.h"

#import "NightMode.h"

@interface MainViewController () <FashionViewControllerDelegate,LeftViewControllerDelegate>
@property (nonatomic,retain) LeftViewController * leftVC;
@property (nonatomic,retain) FashionViewController * fashionVC;
@property (nonatomic, retain)YRSideViewController *sideViewController;
@end

@implementation MainViewController
- (void)dealloc
{
    [_leftVC release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.navigationBar.translucent = NO;
    [self addChild];
    
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    delegate.leftviewController.delegate = self;
    self.sideViewController=[delegate sliderview];
//    _leftVC = [[LeftViewController alloc] init];
//    _leftVC.delegate = self;
    
    
    NSDictionary * dic = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:[self judgeConnection]] forKey:@"Network"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Network" object:nil userInfo:dic]; // 消息中心发送消息，是否有网络连接
}
-(void)leftViewClickWithRow{
    
    [self addFashion];
    
}

// 判断网络是否连接
-(BOOL)judgeConnection{
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    NetworkStatus status = [reach currentReachabilityStatus];
    if(status == NotReachable){ // 没网
        return NO;
    }else if(status == ReachableViaWWAN){ // 手机网
        return YES;
    }else if (status == ReachableViaWiFi){  // wifi
        return YES;
    }
    return NO;
}
-(void)addChild{
    [self addFashion];  // 首先添加fashion到child
//
//    [self addLeft];
}
#pragma mark - FashionViewControllerDelegate
// 点击左上角按钮的弹出child
- (void)callLeftView
{
//    self.tabBarController.tabBar.hidden = YES;
    
    
    
    [_sideViewController showLeftViewController:true];
    
    
    // 创建一个遮挡tabBar的小view，加到FashionView的view上，防止挡住leftVC
    UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 49, self.view.frame.size.width, 49)];
    downView.backgroundColor = [NightMode nightModeON]?[UIColor blackColor]:[UIColor whiteColor];
    [_fashionVC.view addSubview:downView];
    [downView release];
//
//    [UIView animateWithDuration:0.1 animations:^{
//        [_leftVC.view setFrame:CGRectMake(0, 0, self.view.frame.size.width/2+20, self.view.frame.size.height)];
//        
//    } completion:^(BOOL finished) {
//        UIControl *control = [[UIControl alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2+20, 0, self.view.frame.size.width, self.view.frame.size.height)];
//        [control addTarget:self action:@selector(childBack:) forControlEvents:UIControlEventTouchUpInside];
//        [self.view addSubview:control];
//        [control release];
//    }];
}
#pragma mark - 夜间模式设置颜色
// 跟随scrollView的滚动设置left的table的颜色
-(void)setTextColorWithPage:(NSInteger)page{
    for (UITableViewCell * cell in [_leftVC.table visibleCells]) {
        // 如果是夜间模式，设置为亮灰色，如果不是夜间模式，设置为深灰
        cell.textLabel.textColor = [NightMode nightModeON]?[UIColor lightGrayColor]:[UIColor darkGrayColor];
    }
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:page inSection:0];
    UITableViewCell * currentCell = [_leftVC.table cellForRowAtIndexPath:indexPath];
    currentCell.textLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor cyanColor];
}
#if 0
#pragma mark - 让左边的child弹回去
- (void)childBack:(id)sender
{
    self.tabBarController.tabBar.hidden = NO;
    [UIView animateWithDuration:0.1 animations:^{
        [_leftVC.view setFrame:CGRectMake(-self.view.frame.size.width-20, 0, self.view.frame.size.width/2+20, self.view.frame.size.height)];
    } completion:^(BOOL finished) {
        UIControl *control = (UIControl *)sender;
        [control removeFromSuperview];
    }];
    [_fashionVC resetDelegate];
}

#pragma  mark  - 添加viewControllers

-(void)addLeft{
    // 左边的child
    self.leftVC = [[LeftViewController alloc] init];
    _leftVC.delegate = self;
    _leftVC.view.frame = CGRectMake(-self.view.frame.size.width/2-20, 0, self.view.frame.size.width/2+20, self.view.frame.size.height);
    [self addChildViewController:_leftVC];
    [self.view addSubview:_leftVC.view];
    [_leftVC release];
}
#endif
-(void)addFashion{
    self.fashionVC = [[FashionViewController alloc] init];
    _fashionVC.delegate = self;
    [self addChildViewController:_fashionVC];
    [self.view addSubview:_fashionVC.view];
    [_fashionVC release];
}





#pragma  mark - 当试图将要出现时，重新刷新self.view
-(void)viewWillAppear:(BOOL)animated{
    [self.view setNeedsDisplay];
}
@end
